package action;

import entity.Admin;
import entity.User;
import sun.reflect.generics.scope.MethodScope;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import java.io.IOException;

@WebServlet(name = "LogoutAction")
public class LogoutAction extends HttpServlet {
    private static final long serialVersionUID = 4619547987616222442L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------正在执行LogoutAction-------------");
        // 获得session对象
        HttpSession session = request.getSession();
        String method = request.getParameter("method");
        if (method.equals("admin")){
            Admin admin = (Admin) session.getAttribute("admin");
            String name=admin.getAname();
            if (name != null){
                session.removeAttribute("admin");
                System.out.println("管理员注销成功！");
            }
            response.sendRedirect("/class_design/user/user_login.jsp");
        }else if (method.equals("user")){
            User user = (User) session.getAttribute("user");
            String name=user.getUname();
            if (name != null){
                session.removeAttribute("user");
                System.out.println("用户注销成功！");
            }
            response.sendRedirect("/class_design/user/user_login.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
